package com.weaver.drools.common.entity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.*;

import org.hibernate.annotations.GenericGenerator;
/**
 * 
 *  @application name:
 *  @author: zhouxinlei 
 *  @time：2018年6月27日
 *  @version：ver 1.1
 */
@Entity
@Table(name = "sys_permission")
public class SysPermission implements java.io.Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(generator = "snowFlakeId")
	@GenericGenerator(name = "snowFlakeId", strategy = "com.weaver.drools.common.businesskey.SnowFlakeId")
	@Column(name = "PERMISSION_ID",length = 20)
	@OrderBy("PERMISSION_ID asc")
	private Long id;
	
	@Column(name = "PERMISSION_NAME",length = 20)
	private String permissionName;
    
	@Column(name = "PERMISSION_URL",length = 1000)
    private String permissionUrl;
   
    @Column(name = "PERMISSION_CODE",length = 50)
    private String permissionCode;
    
    @Column(name = "DELETE_FLAG", nullable = false, length = 1)
    private Long deleteFlag;//是否过期
    
    @ManyToOne(cascade={CascadeType.REFRESH},fetch=FetchType.EAGER,optional=false)
    @JoinColumn(name = "PARENT_ID", referencedColumnName = "PERMISSION_ID", nullable = false)
    private SysPermission permission;
   
    @OneToMany(cascade = CascadeType.REFRESH, mappedBy = "permission",fetch=FetchType.EAGER)
    @OrderBy("PERMISSION_ID asc")
    private Set<SysPermission> sysPermissionSet  = new TreeSet<SysPermission>();
    @Transient
    private List<SysPermission> permissions;
    
	public SysPermission() {
		super();
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getPermissionName() {
		return permissionName;
	}
	public void setPermissionName(String permissionName) {
		this.permissionName = permissionName;
	}
	public String getPermissionUrl() {
		return permissionUrl;
	}
	public void setPermissionUrl(String permissionUrl) {
		this.permissionUrl = permissionUrl;
	}
	public String getPermissionCode() {
		return permissionCode;
	}
	public void setPermissionCode(String permissionCode) {
		this.permissionCode = permissionCode;
	}
	public SysPermission getPermission() {
		return permission;
	}
	public void setPermission(SysPermission permission) {
		this.permission = permission;
	}
	public Set<SysPermission> getSysPermissionSet() {
		return sysPermissionSet;
	}
	public void setSysPermissionSet(Set<SysPermission> sysPermissionSet) {
		this.sysPermissionSet = sysPermissionSet;
	}
	
	public Long getDeleteFlag() {
		return deleteFlag;
	}
	public void setDeleteFlag(Long deleteFlag) {
		this.deleteFlag = deleteFlag;
	}
	public List<SysPermission> getPermissions() {
		if (sysPermissionSet!=null) {
			List<SysPermission> permissionList= new ArrayList<SysPermission>(sysPermissionSet);
			if (permissionList!=null&&permissionList.size()>0) {
				Collections.sort(permissionList, new Comparator<SysPermission>() {
		            @Override
		            public int compare(SysPermission o1, SysPermission o2) {
		                return o1.getId()<o2.getId() ? -1 :1;
		            }
		        });
			}
			return permissionList;
		}
		return null;
	}
	public void setPermissions(List<SysPermission> permissions) {
		this.permissions =  permissions;
	}
	
}